from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from apps.goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from apps.meiduo_admin.serializers.sku_serializer import SKUSerializers, SKUCategorieSerializer, GoodsSimpleSerializer, \
    GoodsSpecSerializer
from apps.meiduo_admin.utils import PageNum


# 获取sku表数据
class SKUModelViewSet(ModelViewSet):
    serializer_class = SKUSerializers
    pagination_class = PageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword == "" or keyword is None:
            return SKU.objects.all()
        else:
            return SKU.objects.filter(name=keyword)


# 获取三级分类信息
class SKUCateGoryView(ListAPIView):
    serializer_class = SKUCategorieSerializer
    queryset = GoodsCategory.objects.filter(subs=None)


# 获取SPU表名数据
class GoodsSimpleView(ListAPIView):
    serializer_class = GoodsSimpleSerializer
    queryset = SPU.objects.all()


# 获取SPU商品规格信息
class GoodsOptionView(ListAPIView):
    serializer_class = GoodsSpecSerializer

    # 因为我们继承的是ListAPIView，在拓展类中是通过get_queryset获取数据，但是我们现在要获取的是规格信息所以要重写get_queryset类
    def get_queryset(self):
        pk = self.kwargs['pk']
        return SPUSpecification.objects.filter(spu_id=pk)


# 保存数据
class SKUGoodsView(ModelViewSet):
    serializer_class = SKUSerializers
    pagination_class = PageNum

    def get_queryset(self):
        # 提取keyword
        keyword = self.request.query_params.get('keywprd')
        if keyword == "" or keyword is None:
            return SKU.objects.all()
        else:
            return SKU.objects.filter(name=keyword)
